capture program drop regs
program regs

syntax varlist(min=1) [if], link(string) [save(string) fe(varlist) cluster(varname) ///
	ENDOGenous(varlist) INSTRument(varlist) noMARGeff ]

	noi {
	
	/************************************************************************/
	/* 	Define variables														*/
	/************************************************************************/
	
	*Dependent variables
	local depvar: word 1 of `varlist'
	
	*Independent variables
	local indepvars = ""
	local listsize: list sizeof varlist
	
	if `listsize' > 1 {
		forvalues i = 2/`listsize' {
			local var: word `i' of `varlist'
			local indepvars `indepvars' `var'
			}
		}
	local indepvars `indepvars' i.distnum
		
	di "`indepvars'"
		
	/************************************************************************/
	/* 	Run regressions														*/
	/***********************************************************************/
	
	noi di "`link'"
	
	if "`link'" == "lpm" {
		if "`fe'" == "" {
			reg `depvar' `indepvars' `if', cluster(`cluster') 
			}
		if "`fe'" != "" {
			reghdfe `depvar' `indepvars' `if', vce(cluster `cluster') absorb(`fe')
			}
		}
		
	else if "`link'" == "iv" {
		if "`fe'" == "" {
			ivregress 2sls `depvar' `indepvars' `if', cluster(`cluster') 
			}
		if "`fe'" != "" {
			reghdfe `depvar' `indepvars' (`endogenous' = `instrument') `if', vce(cluster `cluster') absorb(`fe')
			}
		}
				
	else {
		if "`fe'" == "" {
			`link' `depvar' `indepvars' `if', robust cluster(`cluster')
			}
		if "`fe'" != "" {
			local xi
			foreach var in `fe' {
				local xi `xi' i.`var'	
				}
			`link' `depvar' `indepvars' `xi' `if', robust cluster(`cluster')
			}
		}
		
		
	local numfires = `e(N_clust)'
				
	if "`margeff'" == "" | "`margeff'" == "margeff" {			
		if "`link'" != "lpm" & "`link'" != "iv" {
			margins, dydx(`indepvars') post
			}
		}
		
	/************************************************************************/
	/* 	Add model descriptions for tables									*/
	/***********************************************************************/
	
	estadd scalar numfires = `numfires'
	estadd local firecontrols "No"
	estadd local othercontrols "No"
	estadd local firefe "No"
	estadd local lags "No"
	distinct directionnum
	estadd scalar L = `r(ndistinct)'
	
	foreach var of varlist `varlist' {
		if "`var'" == "t2F0" estadd local firecontrols "Yes", replace
		if "`var'" == "roadpcttotF0" estadd local othercontrols "Yes", replace
		}
	/* foreach var of varlist *L1 {
		foreach regvar of varlist `varlist' {
			if "`var'" == "`regvar'" estadd local lags "Yes", replace
			}
		} */
	if "`fe'" != "" {
		foreach var of varlist `fe' {
			if "`var'" == "firenum" estadd local firefe "Yes", replace			
			}
		}
		
	if "`save'" != "" est sto `save'
	
	}	
			
end
